//
//  SPMessageFrame.m
//  Sport
//
//  Created by Cass on 2018/4/7.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPMessageFrame.h"
#import "SPMessage.h"
#import <UIKit/UIKit.h>
@implementation SPMessageFrame
- (void)setMessage:(SPMessage *)message{
    _message = message;
    //计算控件的frame和cell高度
    CGFloat SCREEN_W = [UIScreen mainScreen].bounds.size.width;
    //设置统一的间距
    CGFloat margin = 5;
    //设置时间label的frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = SCREEN_W;
    CGFloat timeH = 15;
    
    //如果需要显示时间，再设置时间label的frame
    if (!message.hideTime) {
        _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    }
    //计算头像frame
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX = message.type == SPMessageTypeOther ? margin : (SCREEN_W - margin - iconW);
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    //计算消息frame
    CGSize textSize = [message.text boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    CGFloat textW = textSize.width + 40;
    CGFloat textH = textSize.height + 30;
    CGFloat textY = iconY;
    CGFloat textX = message.type == SPMessageTypeOther ? CGRectGetMaxX(_iconFrame) : (SCREEN_W - margin - iconW - textW);
    _messageFrame = CGRectMake(textX, textY, textW, textH);
    //计算行高
    CGFloat maxY = MAX(CGRectGetMaxY(_iconFrame), CGRectGetMaxY(_messageFrame));
    _cellHeight = maxY + margin;
}
@end
